from django.contrib.auth.models import User
from django.db import models
from utils.modelsMixin import ModelSetMixin
# Create your models here.
from work.models import Lable


class Article(ModelSetMixin):
    TYPE_CHOICES = (
        (0,'未发布'),
        (1,'已发布')
    )
    title = models.CharField(max_length=100,verbose_name='标题')
    digest = models.CharField(max_length=400,verbose_name='摘要')
    content = models.TextField(verbose_name='文章内容')
    page_view = models.IntegerField(verbose_name='浏览量',default=0)
    priority = models.IntegerField(verbose_name="优先级",default=0)
    status = models.IntegerField(verbose_name='状态',default=0,choices=TYPE_CHOICES)
    lable = models.ForeignKey(Lable,on_delete=models.CASCADE)
    user = models.ForeignKey(User,on_delete=models.CASCADE)
    class Meta:
        ordering = ['-priority','-page_view','-create_time']#对数据进行排序，从前到后优先级越大
        db_table = 'article'
        verbose_name = '文章'
        verbose_name_plural = verbose_name
    def __str__(self):
        return f'{self.id}.{self.title}'  # 返回id和文章标题

class Comment(ModelSetMixin):
    content = models.TextField(verbose_name='评论的内容')
    level = models.IntegerField(verbose_name='评论等级', default=1)
    parent_comment = models.IntegerField(verbose_name='父级评论', null=True)
    reply_comment = models.IntegerField(verbose_name='回复评论', null=True)

    article = models.ForeignKey(Article, on_delete=models.CASCADE)
    user = models.ForeignKey(User, on_delete=models.CASCADE)

    class Meta:
        ordering = ['-level','-create_time']
        db_table = 'comment'
        verbose_name = '评论'
        verbose_name_plural = verbose_name

